package top.glike.ssl.quartz.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.glike.ssl.auto.domain.SslDomainCertificate;
import top.glike.ssl.auto.service.IAutoSslConfService;
import top.glike.ssl.auto.service.ISslDomainCertificateService;
import top.glike.ssl.common.utils.StringUtils;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author Sophia
 */
@Slf4j
@Component("ryTask")
public class RyTask {

    @Autowired
    private IAutoSslConfService autoSslConfService;
    @Autowired
    private ISslDomainCertificateService domainCertificateService;

    /**
     * 定时查询需要续期的域名信息
     */
    public void queryNeedRenewCertList() {
        log.info("查询所有证书信息是否需要续期...");
        SslDomainCertificate domain = new SslDomainCertificate();
        domain.setIsDelete(false);
        List<SslDomainCertificate> domainList = domainCertificateService.selectSslDomainCertificateList(domain);
        if (domainList != null && !domainList.isEmpty()) {
            log.info("共获取到{}条证书信息..", domainList.size());
            for (SslDomainCertificate info : domainList) {
                if (info.isExpired() || info.checkNearExpiry()) {
                    log.info("域名{}的证书已过期或即将过期, 符合续期条件, 则进行续期...", info.getDomainName());
                    // 已过期或即将过期，则进行重新续期
                    autoSslConfService.createCert(info.getDomainName());
                }
            }
        }
    }

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }
}
